import * as vscode from 'vscode'

export function getCurrentWorkspacePath(): string | null {
  const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
  return workspaceFolder?.uri.fsPath || null
}

export function getCommentPrefix(filePath: string): string {
  const fileExtension = filePath.split('.').pop()?.toLowerCase()

  switch (fileExtension) {
    case 'py':
    case 'sh':
    case 'bash':
      return '#'
    case 'html':
    case 'htm':
    case 'xml':
    case 'vue':
      return '<!--'
    case 'css':
    case 'scss':
    case 'less':
      return '/*'
    case 'js':
    case 'ts':
    case 'jsx':
    case 'tsx':
    case 'java':
    case 'c':
    case 'cpp':
    case 'h':
    default:
      return '//'
  }
}

/**
 * 生成完整的注释文本
 */
export function generateCommentText(filePath: string, recordId: number, problemTitle: string): string {
  const fileExtension = filePath.split('.').pop()?.toLowerCase()

  switch (fileExtension) {
    case 'py':
    case 'sh':
    case 'bash':
      return `# 🔗 CodeMemo记录 #${recordId}: ${problemTitle}`
    case 'html':
    case 'htm':
    case 'xml':
    case 'vue':
      return `<!-- 🔗 CodeMemo记录 #${recordId}: ${problemTitle} -->`
    case 'css':
    case 'scss':
    case 'less':
      return `/* 🔗 CodeMemo记录 #${recordId}: ${problemTitle} */`
    case 'js':
    case 'ts':
    case 'jsx':
    case 'tsx':
    case 'java':
    case 'c':
    case 'cpp':
    case 'h':
    default:
      return `// 🔗 CodeMemo记录 #${recordId}: ${problemTitle}`
  }
}

/**
 * 获取注释检测的正则表达式
 */
export function getCommentRegex(filePath: string): RegExp {
  const fileExtension = filePath.split('.').pop()?.toLowerCase()

  switch (fileExtension) {
    case 'py':
    case 'sh':
    case 'bash':
      return /^(\s*)#\s*🔗\s+CodeMemo记录\s+#(\d+):(.*)$/
    case 'html':
    case 'htm':
    case 'xml':
    case 'vue':
      return /^(\s*)<!--\s*🔗\s+CodeMemo记录\s+#(\d+):(.*?)\s*-->$/
    case 'css':
    case 'scss':
    case 'less':
      return /^(\s*)\/\*\s*🔗\s+CodeMemo记录\s+#(\d+):(.*?)\s*\*\/$/
    case 'js':
    case 'ts':
    case 'jsx':
    case 'tsx':
    case 'java':
    case 'c':
    case 'cpp':
    case 'h':
    default:
      return /^(\s*)\/\/\s*🔗\s+CodeMemo记录\s+#(\d+):(.*)$/
  }
}

export function truncateText(text: string, maxLength: number = 30): string {
  return text.length > maxLength ? text.substring(0, maxLength) + '...' : text
}
